package warmachineroster.grapheditor.editpart;

import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editpolicies.ComponentEditPolicy;
import org.eclipse.gef.requests.GroupRequest;

import warmachineroster.Roster;
import warmachineroster.RosterElement;
import warmachineroster.grapheditor.command.RosterElementRemoveCommand;
import warmachineroster.grapheditor.figure.BaseRosterElementFigure;
import warmachinesystem.BaseUnit;



public abstract class RosterElementEditPart extends WarmachineRosterAbstractGraphicalEditPart {

	@Override
	protected IFigure createFigure() {
		BaseRosterElementFigure bre = new BaseRosterElementFigure(getElementName(), getUnitType().getArmyList().getCode());
		return bre;
	}


	protected RosterElement getRosterElement() {
		return (RosterElement) getModel();
	}

	protected void createEditPolicies() {
		installEditPolicy(EditPolicy.COMPONENT_ROLE, new ComponentEditPolicy(){
			@Override
			protected Command createDeleteCommand(GroupRequest request){
				RosterElementRemoveCommand command = new RosterElementRemoveCommand();
				command.setRosterElement((RosterElement) getModel());
				command.setRoster((Roster) getParent().getModel());
				return command;
			}
			});
	}

	@Override
	protected void refreshVisuals() {
		BaseRosterElementFigure bre = getRosterElementFigure();
		bre.setFieldAllowance(getUnitType().getFieldAllowance());
		bre.setPointCost(getRosterElement().getPointCost());
	}


	protected BaseUnit getUnitType() {
		return getRosterElement().getUnitType();
	}
	
	protected BaseRosterElementFigure getRosterElementFigure(){
		return (BaseRosterElementFigure) getFigure();
	}

	protected String getElementName() {
		RosterElement elem =  getRosterElement();
		String text = elem.getUnitType() != null? elem.getUnitType().getName(): elem.getClass().getName();
		return text;
	}
}
